package com.shujia.MapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

public class Demo10Question1 {
    public static class MyMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
        Hashtable<String, String> location = null;
        Hashtable<String, String> userPhone = null;

        @Override
        protected void setup(Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
            FileSystem fs = FileSystem.get(context.getConfiguration());
            // 将location.txt文件读取并构建成hashtable
            FSDataInputStream inputStream = fs.open(new Path("/ques1/data2/location.txt"));
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            location = new Hashtable<>();
            while ((line = br.readLine()) != null) {
                String key = line.split(",")[1];
                String value = line.split(",")[2];
                location.put(key, value);
            }
            // userPhone.txt文件读取并构建成hashtable
            BufferedReader br2 = new BufferedReader(new InputStreamReader(fs.open(new Path("/ques1/data2/userPhone.txt"))));
            String line2 = null;
            userPhone = new Hashtable<>();
            while ((line2 = br2.readLine()) != null) {
                String key = line2.split(",")[1];
                String value = line2.split(",")[2];
                userPhone.put(key, value);
            }


        }

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
            // 18620192711,15733218050,1506628174,1506628265,650000,810000
            String[] splits = value.toString().split(",");
            if (splits.length == 6) {
                String caller = splits[0];
                String receiver = splits[1];
                String start_time = splits[2];
                String end_time = splits[3];
                String caller_cid = splits[4];
                String receiver_cid = splits[5];


                String caller_name = userPhone.getOrDefault(caller, "姓名为空");
                String receiver_name = userPhone.getOrDefault(receiver, "姓名为空");
                String caller_city = location.getOrDefault(caller_cid, "姓名为空");
                String receiver_city = location.getOrDefault(receiver_cid, "姓名为空");

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String start_time_str = format.format(new Date(Long.parseLong(start_time)));
                String end_time_str = format.format(new Date(Long.parseLong(end_time)));
                Long sec = Long.parseLong(end_time) - Long.parseLong(start_time);
                context.write(new Text(caller_name + "," + receiver_name + "," + start_time_str + "," + end_time_str + "," + sec + "," + caller_city + "," + receiver_city), NullWritable.get());
            }


        }
    }

    public static class MyReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {

        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJobName("Demo10Question1");
        job.setJarByClass(Demo10Question1.class);

        // 配置Map任务
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);



        // 配置输入输出路径
        FileInputFormat.addInputPath(job, new Path("/ques1/data1/"));
        // 判断输出路径是否存在，存在即删除
        FileSystem fs = FileSystem.get(job.getConfiguration());
        if (fs.exists(new Path("/ques1/res"))) {
            fs.delete(new Path("/ques1/res"), true);
        }
        FileOutputFormat.setOutputPath(job, new Path("/ques1/res"));

        job.waitForCompletion(true);
    }

}
